/**
 * <br>项目名: fastdfs-api
 * <br>文件名: UploadCtrl.java
 * <br>Copyright 2015 恒昌互联网运营中心
 */
package com.credithc.fastdfs.api.web;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.credithc.fastdfs.api.bean.ResultBean;
import com.credithc.fastdfs.api.bean.UploadReqBean;
import com.credithc.fastdfs.api.bean.UploadResBean;
import com.credithc.fastdfs.api.config.SystemConfig;
import com.credithc.fastdfs.api.exception.UploadException;
import com.credithc.fastdfs.api.service.FileUploadService;
import com.credithc.framework.core.utils.FastJsonConvert;

/**
 * <br>
 * 类 名: UploadCtrl <br>
 * 描 述: 描述类完成的主要功能 <br>
 * 作 者: zj <br>
 * 创 建： 2015年5月11日 <br>
 * 版 本：v1.0.0 <br>
 * <br>
 * 历 史: (版本) 作者 时间 注释
 */
@Controller
@RequestMapping("/uploadFile.hc")
public class UploadCtrl extends BaseController {
	private Logger log = LoggerFactory.getLogger(UploadCtrl.class);
	public static final String SUCCESS = "1";// 上传成功
	public static final String FAILED = "0";// 上传失败

	@Resource(name = "fileUploadService")
	private FileUploadService fileUploadService;


	/**
	 * 上传文件
	 */
	@RequestMapping(method = { RequestMethod.POST })
	public String upload(MultipartHttpServletRequest request,
			HttpServletResponse response, UploadReqBean uploadReqBean)
			throws IOException {
		UploadResBean uploadResBean = new UploadResBean();
		uploadResBean.setFsId("");
		uploadResBean.setSlaveName("");
		ResultBean resultBean=new ResultBean();
		resultBean.setResult("1");
		resultBean.setErrorMsg("");
		resultBean.setData(uploadResBean);
		try {
			if (handler(uploadReqBean, uploadResBean)) { // 文件上传操作成功
				uploadResBean.setStatus(UploadCtrl.SUCCESS);
				uploadResBean.setMessage("上传成功");
				uploadResBean.setSlaveName(uploadReqBean.getSlaveName());
				resultBean.setData(uploadResBean);
			}
		} catch (UploadException ex) {
			uploadResBean = new UploadResBean();
			HttpStatus status = ex.getHttpStatus();
			String message = ex.getMessage();
//			response.setStatus(status.value());
			uploadResBean.setStatus(UploadCtrl.FAILED);
			uploadResBean.setMessage("文件上传异常");
			resultBean.setData(uploadResBean);
			log.error(message, ex);
			addError(ex.getMessage(), MOD_UPLOAD, "/");
		}
		return ajaxHtml(FastJsonConvert.convertObjectToJSON(resultBean),
				response);
	}

	/**
	 * 处理文件上传
	 * 
	 * @throws UploadException
	 */
	public boolean handler(UploadReqBean uploadReqBean,
			UploadResBean uploadResBean) throws UploadException {
		
			MultipartFile file = uploadReqBean.getFile();
			
			if (file == null || file.getSize() == 0) {
				throw new UploadException("文件内容为空", HttpStatus.UNPROCESSABLE_ENTITY);
			}
			if(StringUtils.isBlank(uploadReqBean.getFileType())) {
				throw new UploadException("文件类型为空", HttpStatus.UNPROCESSABLE_ENTITY);
			}
			if(StringUtils.isBlank(uploadReqBean.getFileName())) {
				throw new UploadException("原始文件名为空", HttpStatus.UNPROCESSABLE_ENTITY);
			}
			String orgFileName = uploadReqBean.getFileName();
			orgFileName = (orgFileName == null) ? "" : orgFileName;
			
			if (StringUtils.equalsIgnoreCase(uploadReqBean.getFileType(), SystemConfig.SLAVE_TYPE_DOC)) {// 判断是否文档类型
				boolean f = fileUploadService.uploadTypeFile(uploadReqBean,
						uploadResBean, request);//文档类型上传
				if (!f) {
					addError(uploadResBean.getMessage(), MOD_UPLOAD,
							orgFileName);
				}
				return f;
			} else if (StringUtils.equalsIgnoreCase(uploadReqBean.getFileType(), SystemConfig.SLAVE_TYPE_IMG)) { // 判断是否图片类型
				boolean f = fileUploadService.uploadTypeImg(uploadReqBean,
						uploadResBean, request);//图片类型上传
				if (!f) {
					addError(uploadResBean.getMessage(), MOD_UPLOAD,
							orgFileName);
				}
				return f;
			} else {
				uploadResBean.setStatus(UploadCtrl.FAILED);
				uploadResBean.setMessage("文件类型错误");
			}
		
		addError(uploadResBean.getMessage(), MOD_UPLOAD, "/");
		return false;
	}

}
